package com.fgxia.user.controller;

import com.fgxia.user.domain.bo.UserInvoiceInfoBo;
import com.fgxia.user.domain.vo.OrderInvoiceVo;
import com.fgxia.user.domain.vo.UserInvoiceInfoVo;
import com.fgxia.user.service.IInvoiceInfoService;
import com.ruoyi.business.domain.InvoiceInfo;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 发票信息
 *
 * @author yyt
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/invoice")
public class InvoiceInfoController extends BaseController {

    private final IInvoiceInfoService invoiceInfoService;

    /**
     * 查询可开票订单
     */
    @GetMapping("/order/list")
    public TableDataInfo<OrderInvoiceVo> listOrder(PageQuery pageQuery) {
        TableDataInfo<OrderInvoiceVo> result = invoiceInfoService.selectInvoiceOrderByUserId(getUserId(), pageQuery);
        return result;
    }

    /**
     * 查询已开票详情
     */
    @GetMapping("/{invoiceId}")
    public R<UserInvoiceInfoVo> getDetail(@PathVariable Long invoiceId) {
        InvoiceInfo invoiceInfo = new InvoiceInfo();
        invoiceInfo.setInvoiceId(invoiceId);
        invoiceInfo.setRelevanceUserId(getUserId());
        UserInvoiceInfoVo invoiceInfoVo = invoiceInfoService.selectOne(invoiceInfo);
        return R.ok(invoiceInfoVo);
    }

    /**
     * 查询已开票记录
     */
    @GetMapping("/list")
    public TableDataInfo<UserInvoiceInfoVo> list(PageQuery pageQuery) {
        TableDataInfo<UserInvoiceInfoVo> tableDataInfo = invoiceInfoService.selectInvoicedByUserId(getUserId(), pageQuery);
        return tableDataInfo;
    }

    /**
     * 开票
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> invoice(@RequestBody UserInvoiceInfoBo userInvoiceInfoBo) {
        // todo 开票的公司名称和统一社会信用代码、税号，应该从个人信息里拿，临时变为手动输入，待优化
        userInvoiceInfoBo.setRelevanceUserId(getUserId());
        return toAjax(invoiceInfoService.createInvoices(userInvoiceInfoBo));
    }
}
